/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.HljgKs;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.domain.sup.Rygl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.HljgKsService;
import me.zhengjie.business.service.HljgService;
import me.zhengjie.business.service.RyglService;
import me.zhengjie.business.service.dto.HljgKsQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-08-10
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "机构科室管理")
@RequestMapping("/api/hljgKs")
public class HljgKsController {

    private final HljgKsService hljgKsService;

    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('hljgKs:list')")
    public void exportHljgKs(HttpServletResponse response, HljgKsQueryCriteria criteria) throws IOException {
        hljgKsService.download(hljgKsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询机构科室")
    @ApiOperation("查询机构科室")
    @PreAuthorize("@el.check('hljgKs:list')")
    public ResponseEntity<Object> queryHljgKs(HljgKsQueryCriteria criteria, Pageable pageable){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisHljgKsHlId(gsgl.getBisGsglId());
        return new ResponseEntity<>(hljgKsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllHljgKs")
    @ApiOperation("查询所有护理机构科室")
    public ResponseEntity<Object> queryAllHljgKs(HljgKsQueryCriteria criteria){

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisHljgKsHlId(gsgl.getBisGsglId());

        return new ResponseEntity<>(hljgKsService.queryAll(criteria),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增机构科室")
    @ApiOperation("新增机构科室")
    @PreAuthorize("@el.check('hljgKs:add')")
    public ResponseEntity<Object> createHljgKs(@Validated @RequestBody HljgKs resources){

        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        resources.setBisHljgKsHlId(gsgl.getBisGsglId());
        return new ResponseEntity<>(hljgKsService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改机构科室")
    @ApiOperation("修改机构科室")
    @PreAuthorize("@el.check('hljgKs:edit')")
    public ResponseEntity<Object> updateHljgKs(@Validated @RequestBody HljgKs resources){
        hljgKsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除机构科室")
    @ApiOperation("删除机构科室")
    @PreAuthorize("@el.check('hljgKs:del')")
    public ResponseEntity<Object> deleteHljgKs(@RequestBody String[] ids) {
        hljgKsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}